//
//  ZLAlertButton.swift
//  ZLAlert
//
//  Created by 赵振磊 on 2022/11/27.
//

import Foundation

/// 按钮
extension ZLAlert: ZLAlertDoubleButtonDelegate {
    /// 添加双按钮
    /// - Parameters:
    ///   - leftTitle: 左侧按钮标题
    ///   - leftDismiss: 左侧按钮点击是否自动消失
    ///   - leftConfig: 左侧按钮配置
    ///   - leftCallTextField: 左侧按钮是否调用TextField
    ///   - leftAction: 左侧按钮点击事件
    ///   - centerLine: 是否显示中间分割线
    ///   - rightTitle: 右侧按钮标题
    ///   - rightDismiss: 右侧按钮点击是否自动消失
    ///   - rightConfig: 右侧按钮配置
    ///   - rightCallTextField: 右侧按钮是否调用TextField
    ///   - rightAction: 右侧按钮点击事件
    /// - Returns: ZLAlert
    @discardableResult
    public func addDoubleAction(z_left leftTitle: String,
                                leftCallTextField: Bool = false,
                                _ leftAction: ZlAlertAction?,
                                z_line centerLine: Bool,
                                z_right rightTitle: String,
                                rightCallTextField: Bool = false,
                                _ rightAction: ZlAlertAction?) -> ZLAlert {
        return self.addDoubleAction(z_left: leftTitle, true,
                                    ZLAlertConfig.shared.cancelButtonConfig,
                                    leftCallTextField: leftCallTextField,
                                    leftAction,
                                    z_line: centerLine,
                                    ZLAlertConfig.shared.lineConfig,
                                    z_right: rightTitle,
                                    true, ZLAlertConfig.shared.buttonConfig,
                                    rightCallTextField: rightCallTextField,
                                    rightAction)
    }
    /// 添加双按钮
    /// - Parameters:
    ///   - leftTitle: 左侧按钮标题
    ///   - leftDismiss: 左侧按钮点击是否自动消失
    ///   - leftConfig: 左侧按钮配置
    ///   - leftCallTextField: 左侧按钮是否调用TextField
    ///   - leftAction: 左侧按钮点击事件
    ///   - centerLine: 是否显示中间分割线
    ///   - lineConfig: 分割线的配置
    ///   - rightTitle: 右侧按钮标题
    ///   - rightDismiss: 右侧按钮点击是否自动消失
    ///   - rightConfig: 右侧按钮配置
    ///   - rightCallTextField: 右侧按钮是否调用TextField
    ///   - rightAction: 右侧按钮点击事件
    /// - Returns: ZLAlert
    @discardableResult
    public func addDoubleAction(z_left leftTitle: String,
                                _ leftDismiss: Bool,
                                _ leftConfig: ZLAlertItemConfig,
                                leftCallTextField: Bool = false,
                                _ leftAction: ZlAlertAction?,
                                z_line centerLine: Bool,
                                _ lineConfig: ZLAlertItemConfig,
                                z_right rightTitle: String,
                                _ rightDismiss: Bool,
                                _ rightConfig: ZLAlertItemConfig,
                                rightCallTextField: Bool = false,
                                _ rightAction: ZlAlertAction?) -> ZLAlert {
        let lastView = self.mainView.subviews.last
        let doubleButton = ZLAlertDoubleButton.init(leftTitle, leftDismiss, leftConfig, leftCallTextField, leftAction, centerLine, lineConfig, rightTitle, rightDismiss, rightConfig, rightCallTextField, rightAction)
        doubleButton.delegate = self
        self.mainView.addSubview(doubleButton)
        doubleButton.snp.makeConstraints { make in
            make.left.right.equalTo(self.mainView)
            if (lastView == nil) {
                make.top.equalTo(self.mainView)
            } else {
                make.top.equalTo(lastView!.snp.bottom)
            }
            make.height.equalTo(leftConfig.height > rightConfig.height ? leftConfig.height : rightConfig.height)
        }
        return self.layoutMainView(0)
    }
    /// 添加单按钮
    /// - Parameters:
    ///   - title: 标题
    ///   - action: 事件
    /// - Returns: ZLAlert
    @discardableResult
    public func addButton(_ title: String,
                          callTextField: Bool = false,
                          action: ZlAlertAction?) -> ZLAlert {
        return self.addButton(title: title, dismiss: true, config: ZLAlertConfig.shared.buttonConfig, callTextField: callTextField, action: action)
    }
    /// 添加单按钮
    /// - Parameters:
    ///   - title: 标题
    ///   - dismiss: 点击是否自动消失
    ///   - config: 配置
    ///   - action: 事件
    /// - Returns: ZLAlert
    @discardableResult
    public func addButton(title: String,
                          dismiss: Bool = true,
                          config: ZLAlertItemConfig,
                          callTextField: Bool = false,
                          action: ZlAlertAction? = nil) -> ZLAlert {
        let lastView = self.mainView.subviews.last
        let button = self.makeButton(title, dismiss, config, callTextField, action)
        button.snp.makeConstraints { make in
            make.left.equalTo(self.mainView).offset(config.insets.left)
            make.right.equalTo(self.mainView).offset(-config.insets.right)
            if (lastView == nil) {
                make.top.equalTo(self.mainView).offset(config.insets.top)
            } else {
                make.top.equalTo(lastView!.snp.bottom).offset(config.insets.top)
            }
            make.height.equalTo(config.height)
        }
        return self.layoutMainView(0)
    }
    
    /// 创建按钮
    /// - Parameters:
    ///   - title: 标题
    ///   - dismiss: 点击是否自动消失
    ///   - config: 配置
    ///   - callTextField: 是否调用TextField
    ///   - action: 事件
    /// - Returns: ZLAlertButton
    func makeButton(_ title: String,
                    _ dismiss: Bool,
                    _ config: ZLAlertItemConfig,
                    _ callTextField: Bool = false,
                    _ action: ZlAlertAction?) -> ZLAlertButton {
        let button = ZLAlertButton.zl_button(title, dismiss, config, callTextField, action)
        button.addTarget(self, action: #selector(zl_action(button:)), for: .touchUpInside)
        self.mainView.addSubview(button)
        return button
    }
    /// ZLAlertDoubleButtonDelegate
    func alertDoubleButtonTapLeft(button: ZLAlertButton) {
        self.zl_action(button: button)
    }
    func alertDoubleButtonTapRight(button: ZLAlertButton) {
        self.zl_action(button: button)
    }
    /// 点击事件
    @objc func zl_action(button: ZLAlertButton) {
        if button.callTextField {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ZLAlertCallTextField"), object: nil)
        }
        if (button.dismiss) {
            self.dismiss(animated: true) {
                button.action?()
            }
            return
        }
        button.action?()
    }
}

protocol ZLAlertDoubleButtonDelegate: NSObject {
    func alertDoubleButtonTapLeft(button: ZLAlertButton) -> Void
    func alertDoubleButtonTapRight(button: ZLAlertButton) -> Void
}
public class ZLAlertDoubleButton: UIView {
    /// 左侧按钮
    public var leftButton: ZLAlertButton?
    /// 右侧按钮
    public var rightButton: ZLAlertButton?
    /// 中间分割线
    public var centerLine: UIView?
    /// 代理
    weak var delegate: ZLAlertDoubleButtonDelegate?

    /// 初始化
    /// - Parameters:
    ///   - leftTitle: 左侧按钮标题
    ///   - leftDismiss: 左侧按钮点击是否自动消失
    ///   - leftConfig: 左侧按钮配置
    ///   - leftCallTextField: 左侧按钮是否调用TextField
    ///   - leftAction: 左侧按钮点击事件
    ///   - centerLine: 是否显示中间分割线
    ///   - lineConfig: 分割线的配置
    ///   - rightTitle: 右侧按钮标题
    ///   - rightDismiss: 右侧按钮点击是否自动消失
    ///   - rightConfig: 右侧按钮配置
    ///   - rightCallTextField: 右侧按钮是否调用TextField
    ///   - rightAction: 右侧按钮点击事件
    init(_ leftTitle: String,
         _ leftDismiss: Bool,
         _ leftConfig: ZLAlertItemConfig,
         _ leftCallTextField: Bool = false,
         _ leftAction: ZlAlertAction?,
         _ centerLine: Bool,
         _ lineConfig: ZLAlertItemConfig,
         _ rightTitle: String,
         _ rightDismiss: Bool,
         _ rightConfig: ZLAlertItemConfig,
         _ rightCallTextField: Bool = false,
         _ rightAction: ZlAlertAction?) {
        super.init(frame: CGRect.zero)
        self.leftButton = ZLAlertButton.zl_button(leftTitle, leftDismiss, leftConfig, leftCallTextField, leftAction)
        self.leftButton!.addTarget(self, action: #selector(self.leftAction), for: .touchUpInside)
        self.addSubview(self.leftButton!)
        self.leftButton!.snp.makeConstraints { make in
            make.top.bottom.equalTo(self)
            make.left.equalTo(self).offset(leftConfig.insets.left)
            make.right.equalTo(self.snp.centerX).offset(-leftConfig.insets.right)
        }
        self.rightButton = ZLAlertButton.zl_button(rightTitle, rightDismiss, rightConfig, rightCallTextField, rightAction)
        self.rightButton!.addTarget(self, action: #selector(self.rightAction), for: .touchUpInside)
        self.addSubview(self.rightButton!)
        self.rightButton!.snp.makeConstraints { make in
            make.top.bottom.equalTo(self)
            make.left.equalTo(self.snp.centerX).offset(rightConfig.insets.left)
            make.right.equalTo(self).offset(-rightConfig.insets.right)
        }
        if (centerLine == false) {
            return
        }
        self.centerLine = UIView()
        self.centerLine!.backgroundColor = lineConfig.backgroundColor
        self.addSubview(self.centerLine!)
        self.centerLine!.snp.makeConstraints { make in
            make.centerX.top.bottom.equalTo(self)
            make.width.equalTo(lineConfig.height)
        }
    }
    
    @objc func leftAction() {
        self.delegate?.alertDoubleButtonTapLeft(button: self.leftButton!)
    }
    
    @objc func rightAction() {
        self.delegate?.alertDoubleButtonTapRight(button: self.rightButton!)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
